package shake.util;

import java.lang.reflect.Field;

import javassist.util.proxy.ProxyObject;

public class Reflections {

	static public boolean isWrapClass(Object object) {
		Class clazz = object.getClass();
		if (object instanceof ProxyObject) {
			return true;
		}
		if (clazz.getName().matches(".*EnhancerByGuice.*")) {
			return true;
		}
		return false;
	}

	static public Class<?> unwrapClass(Object object) {
		if (isWrapClass(object)) {
			return object.getClass().getSuperclass();
		}
		return object.getClass();
	}

	static public Object get(Object obj, String fieldName) {
		try {
			if (obj == null) {
				return null;
			}
			Field f = obj.getClass().getField(fieldName);
			f.setAccessible(true);
			Object o = f.get(obj);
			f.setAccessible(false);
			return o;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
